// IElasticsearchService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Beauty.Net.Elasticsearch
{
    /// <summary>
    /// Elasticsearch服务接口
    /// </summary>
    public interface IElasticsearchService
    {
        /// <summary>
        /// 索引单个文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="document">文档对象</param>
        /// <param name="id">文档ID（可选）</param>
        /// <returns>操作结果</returns>
        Task<IndexResult> IndexAsync<T>(string indexName, T document, string? id = null) where T : class;

        /// <summary>
        /// 批量索引文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="documents">文档集合</param>
        /// <returns>批量操作结果</returns>
        Task<BulkResult> BulkIndexAsync<T>(string indexName, IEnumerable<T> documents) where T : class;

        /// <summary>
        /// 获取单个文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="id">文档ID</param>
        /// <returns>文档对象或null</returns>
        Task<T?> GetAsync<T>(string indexName, string id) where T : class;

        /// <summary>
        /// 更新文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="id">文档ID</param>
        /// <param name="document">更新后的文档对象</param>
        /// <returns>操作结果</returns>
        Task<UpdateResult> UpdateAsync<T>(string indexName, string id, T document) where T : class;

        /// <summary>
        /// 删除文档
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <param name="id">文档ID</param>
        /// <returns>操作结果</returns>
        Task<DeleteResult> DeleteAsync(string indexName, string id);

        /// <summary>
        /// 搜索文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="query">搜索查询（可以是JSON字符串或对象）</param>
        /// <param name="from">起始位置</param>
        /// <param name="size">返回数量</param>
        /// <returns>搜索结果</returns>
        Task<SearchResult<T>> SearchAsync<T>(string indexName, object query, int from = 0, int size = 10) where T : class;

        /// <summary>
        /// 简单文本搜索
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="field">搜索字段</param>
        /// <param name="value">搜索值</param>
        /// <param name="from">起始位置</param>
        /// <param name="size">返回数量</param>
        /// <returns>搜索结果</returns>
        Task<SearchResult<T>> SimpleSearchAsync<T>(string indexName, string field, string value, int from = 0, int size = 10) where T : class;

        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <param name="settings">索引设置（可选）</param>
        /// <returns>操作结果</returns>
        Task<bool> CreateIndexAsync(string indexName, object? settings = null);

        /// <summary>
        /// 删除索引
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns>操作结果</returns>
        Task<bool> DeleteIndexAsync(string indexName);

        /// <summary>
        /// 检查索引是否存在
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns>是否存在</returns>
        Task<bool> IndexExistsAsync(string indexName);
    }

    /// <summary>
    /// 索引操作结果
    /// </summary>
    public class IndexResult
    {
        public string? Id { get; set; }
        public bool IsSuccess { get; set; }
        public string? Error { get; set; }
    }

    /// <summary>
    /// 批量操作结果
    /// </summary>
    public class BulkResult
    {
        public int SuccessCount { get; set; }
        public int FailedCount { get; set; }
        public bool IsSuccess { get; set; }
        public List<string>? Errors { get; set; }
    }

    /// <summary>
    /// 更新操作结果
    /// </summary>
    public class UpdateResult
    {
        public string Id { get; set; }
        public bool IsSuccess { get; set; }
        public string? Error { get; set; }
    }

    /// <summary>
    /// 删除操作结果
    /// </summary>
    public class DeleteResult
    {
        public string Id { get; set; }
        public bool IsSuccess { get; set; }
        public string? Error { get; set; }
    }

    /// <summary>
    /// 搜索结果
    /// </summary>
    public class SearchResult<T> where T : class
    {
        public List<T> Documents { get; set; } = new List<T>();
        public long Total { get; set; }
        public int Took { get; set; }
        public bool IsSuccess { get; set; }
        public string? Error { get; set; }
    }
}